package com.baizan.daka.ui.presenter

import com.baizan.daka.base.BasePresenter
import com.baizan.daka.ui.contract.RegisterContract
import com.baizan.daka.mvp.model.UserModel
import com.baizan.daka.mvp.model.bean.QuestionBean
import com.baizan.daka.mvp.model.bean.RegistInfoBean
import com.baizan.daka.net.bean.FindPasswordBean
import com.baizan.daka.net.bean.RegistBean
import com.baizan.daka.rx.Subscriber.CommonSubscriber
import com.baizan.daka.showToast
import com.baizan.daka.util.Md5

class RegisterPresenter(val mView: RegisterContract.View) : BasePresenter(),
    RegisterContract.Presenter {


    private val userModel: UserModel by lazy {
        UserModel()
    }


    override fun regist(
        account: String,
        password: String,
        invitationCode: String,
        questionCode: String,
        questionName: String,
        questionAnwser: String
    ) {
        mView.showLoadingDialog()
        userModel.register(
                RegistBean(
                    account,
                    password,
                    questionCode,
                    questionName,
                    questionAnwser,
                    invitationCode
                )
            )
            .subscribe(object : CommonSubscriber<String>(compositeDisposable) {

                override fun onCallNext(t: String?) {
                    showToast("注册成功")
                    mView.registSuccess(t!!)
                }

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    showToast(e)
                }

                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }
            })
    }

    override fun sendCode(phone: String) {
        mView.showLoadingDialog()
        userModel.registerCode(phone)
            .subscribe(object : CommonSubscriber<RegistInfoBean>(compositeDisposable) {

                override fun onCallNext(t: RegistInfoBean?) {
                    mView.sendSuccess(t!!)
                }

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    showToast(e)
                }

                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }
            })
    }

    override fun getQuestionList() {
        mView.showLoadingDialog()
        userModel.passwordQuestion()
            .subscribe(object : CommonSubscriber<List<QuestionBean>>(compositeDisposable) {
                override fun onCallNext(t: List<QuestionBean>?) {
                    mView.getQuestionListSuccess(t!!)
                }

                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    showToast("密保问题获取失败")
                }
            })
    }

    override fun findPassword(
        account: String,
        password: String,
        questionCode: String,
        questionName: String,
        questionAnwser: String
    ) {
        mView.showLoadingDialog()
        userModel.findPassword(
                FindPasswordBean(
                    account,
                    password,
                    questionCode,
                    questionName,
                    questionAnwser
                )
            )
            .subscribe(object : CommonSubscriber<Any>(compositeDisposable) {

                override fun onCallNext(t: Any?) {
                    showToast("注册成功")
                    mView.findSuccess()
                }

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    showToast(e)
                }

                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }
            })
    }


}

